package com.gl.glbooks.util;

import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

import com.gl.glbooks.services.IMailServices;
import com.gl.glbooks.vos.Libro;
import com.gl.glbooks.vos.LibroDigital;
import com.gl.glbooks.vos.LibroFisico;
import com.gl.glbooks.vos.Sugerencia;
import com.gl.glbooks.vos.Usuario;

public class HiloMail extends Thread{
	
	private JavaMailSender mailSender;
	private VelocityEngine velocityEngine;
	private Usuario user;
	private String mensaje;
	private Long id;
	private Libro libro=null;
	private String smsRechazado="";
	private String code="";
	private String url;
	private Usuario from;
	private IMailServices mailService;
	private String activar="";
	private Sugerencia sugerencia=null;
	
	
	public IMailServices getMailService() {
		return mailService;
	}


	public void setMailService(IMailServices mailService) {
		this.mailService = mailService;
	}


	public HiloMail(String url, String code, Usuario user, String mensaje,JavaMailSender mailSender, VelocityEngine velocityEngine){
		this.url=url;
		this.code=code;
		this.user=user;
		this.mensaje=mensaje;
		this.mailSender=mailSender;
		this.velocityEngine=velocityEngine;
	}
	
	public HiloMail(Usuario user, String mensaje, JavaMailSender mailSender, VelocityEngine velocityEngine){
		this.user=user;
		this.id=user.getId();
		this.mensaje= mensaje;
		this.mailSender=mailSender;
		this.velocityEngine=velocityEngine;
	}
	
	public HiloMail(Usuario user, String mensaje, JavaMailSender mailSender, VelocityEngine velocityEngine, String url, Sugerencia sugerencia){
		this.user=user;
		this.id=user.getId();
		this.mensaje= mensaje;
		this.mailSender=mailSender;
		this.velocityEngine=velocityEngine;
		this.url=url;
		this.activar = "activarUrl";
		this.sugerencia = sugerencia;
	}
	
	public HiloMail(String url, Libro libro, Usuario user, String mensaje, JavaMailSender mailSender, VelocityEngine velocityEngine){
		this.user=user;
		this.mensaje= mensaje;
		this.mailSender=mailSender;
		this.velocityEngine=velocityEngine;
		this.libro = libro;
		this.url = url;		
	}
	
	public HiloMail(String url, Long id, Usuario user, Usuario from, String mensaje, JavaMailSender mailSender, VelocityEngine velocityEngine){
		this.user=user;
		this.mensaje= mensaje;
		this.mailSender=mailSender;
		this.velocityEngine=velocityEngine;
		this.id = id;
		this.url = url;
		this.from = from;
	}
	
	public HiloMail(String smsRechazado, Usuario user, String mensaje, JavaMailSender mailSender, VelocityEngine velocityEngine){
		this.user=user;
		this.mensaje= mensaje;
		this.mailSender=mailSender;
		this.velocityEngine=velocityEngine;
		this.smsRechazado = smsRechazado;
		this.code=smsRechazado;
	}	

	public void run(){
	      System.out.println("Thread Running: funciono");  
	      MimeMessagePreparator preparator = new MimeMessagePreparator() {
		         public void prepare(MimeMessage mimeMessage)  {
		            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
		            try {
		            	message.setTo(user.getCorreoElectronico());
			            message.setFrom("robot@globallogic.com");
			            if(user.isIngles()){
			            	message.setSubject("Library");
			            }else{
			            	message.setSubject("Biblioteca");
			            }
			            Map model = new HashMap();
			            model.put("user", user);
			            model.put("from", from);
			            
			            if(!smsRechazado.equalsIgnoreCase(""))
			            model.put("smsRechazado", smsRechazado);
			            
			            if(!code.equalsIgnoreCase(""))
				            model.put("code", code);
			            
			            if( libro != null )
			            {
			            	model.put("libroURL", url+libro.getId());
			            	model.put("libro", libro);
			            	if (libro instanceof LibroDigital) {
			            		model.put("formato", "Libro Digital");
							}else{
								model.put("formato", "Libro Fisico");
							}
						}
			            if( id != null )
			            {
			            	model.put("libroURL", url+ id );
			            }
			            
			            if(activar.equalsIgnoreCase("activarUrl")){
			            	model.put("urlSugerencia", url+sugerencia.getId());
			            }
			            
			            String text = VelocityEngineUtils.mergeTemplateIntoString(
			               velocityEngine, "mail/"+mensaje, model);
			            message.setText(text, true);  
		            } catch (MessagingException e) {
		            	e.printStackTrace();
		            }
		         }
		         
		      };
		      mailSender.send(preparator);     
			  mailService.mailSent();
	    }

}
